package masterjava.common.client.gxt.highstock;

/**
 * User: gkislin
 * Date: 7/7/11
 */

import com.extjs.gxt.ui.client.widget.BoxComponent;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.HTMLPanel;
import masterjava.common.client.gxt.base.util.JsonObject;
import masterjava.common.shared.util.SharedUtil;

public class HighStockChart extends BoxComponent implements HighStockChartGetter{
    private HighStockSeries[] seriesArray;
    private JSONArray jsonSeries = new JSONArray();
    private final JsonObject params;
    private boolean loaded = false;

    public HighStockChart(HighStockType type) {
        super();
        super.setLayoutData(new FitLayout());
        this.params = JsonObject.getClone(type.getParams());
    }

    @Override
    protected void onRender(Element target, int index) {
        Element chartDiv = DOM.createDiv();
        chartDiv.setAttribute("style", "height: 500px");
        chartDiv.setId(HTMLPanel.createUniqueId());
        setElement(chartDiv, target, index);
        super.onRender(target, index);
        setProperty("chart/renderTo", chartDiv.getId());
        loaded = true;
        update();
    }

    public JSONValue getProperty(String path) {
        return params.getProperty(path);
    }

    public void setProperty(String path, Object value) {
        params.setProperty(path, value);
    }

    public JsonObject getParams() {
        return params;
    }

    public void setType(HighStockSeriesType type) {
        if (!SharedUtil.isEmpty(seriesArray)) {
            for (HighStockSeries seriesItem : seriesArray) {
                seriesItem.setType(type);
            }
            update();
        }
    }

    public void setSeries(HighStockSeries... seriesArray) {
        this.seriesArray = seriesArray;
        if (seriesArray == null) {
            return;
        }
        int i = 0;
        for (HighStockSeries item : seriesArray) {
            jsonSeries.set(i, item);
            i++;
        }
        update();
    }

    private void update() {
        if (loaded) {
            if (!SharedUtil.isEmpty(seriesArray)) {
                for (HighStockSeries seriesItem : seriesArray) {
                    // prepare json to rendering
                    // json is spoiled after rendering, so recover data
                    seriesItem.setDataBeforeDraw();
                }
                setProperty("series", jsonSeries);
                drawChart(params.getJavaScriptObject());
            }
        }
    }

    @Override
    protected void onUnload() {
        super.onUnload();
        loaded = false;
    }

    private native void drawChart(JavaScriptObject chartParam) /*-{
        new $wnd.Highcharts.StockChart(chartParam);
    }-*/;

    //  Resize not solved yet
    @Override
    public void recalculate() {
        super.recalculate();
    }

    @Override
    public HighStockChart getChart() {
        return this;
    }
}
